package com.notifier;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class EmailUtility {
	
	private AppContext ctx;
	
	public EmailUtility(AppContext ctx){
		this.ctx = ctx;
	}
	
	public void sendEmail(){
		String host = "smtp.gmail.com";
		int port = 587;
		
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		
		Session session = Session.getInstance(props);
		
		try{
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("from@no-spam.com"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(ctx.emailAddress));
			message.setSubject("Surly Tours are Now Open!!!");
			message.setText("Book Now!");
			
			Transport transport = session.getTransport("smtp");
			transport.connect(host, port, ctx.username, ctx.password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		
			System.out.println("Email Sent");
		}
		catch(MessagingException e){
			e.printStackTrace();
		}
	}
	
}

